package com.daiqile.myinterest.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.daiqile.myinterest.BaseActivity;
import com.daiqile.myinterest.R;
import com.daiqile.myinterest.customview.ViewPagerFix;
import com.daiqile.myinterest.fragement.OneFragment;
import com.daiqile.myinterest.fragement.ThreeFragment;
import com.daiqile.myinterest.fragement.TwoFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainViewActivity extends BaseActivity {
    private ViewPagerAdapter adapter;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private List<Fragment> fragment = new ArrayList<>();
    private long clickTime = 0;//点击时间
    @BindView(R.id.viewPage)
    ViewPagerFix viewPage;
    @BindView(R.id.bottomNavigation)
    BottomNavigationBar bottomNavigation;

    @Override
    protected int getContentView() {
        return R.layout.activity_main_view;
    }

    @Override
    protected void initView() {
//        RxBarTool.setTransparentStatusBar(this);
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fragment.add(oneFragment);
        fragment.add(twoFragment);
        fragment.add(threeFragment);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        //set Viewpage
        viewPage.setAdapter(adapter);
        viewPage.setScrollable(false);
        viewPage.setOffscreenPageLimit(fragment.size());
        //set BottomNavigationBar
        bottomNavigation.setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
                .setActiveColor(R.color.material_white)//在BACKGROUND_STYLE_STATIC 模式下颜色是图标和文字的颜色（选中/未选中），在BACKGROUND_STYLE_RIPPLE 模式下是底部导航栏背景色。
                .setInActiveColor(R.color.material_blueGrey_700)
                .setBarBackgroundColor(R.color.tabtext);//在BACKGROUND_STYLE_STATIC 模式下颜色是底部导航栏背景色，BACKGROUND_STYLE_RIPPLE模式下是图标和文字的颜色（选中/未选中）

        BottomNavigationItem item1 = new BottomNavigationItem(R.drawable.icon_one_s, getString(R.string.main)).setInactiveIconResource(R.drawable.icon_one_n).setActiveColorResource(R.color.material_white);
        BottomNavigationItem item2 = new BottomNavigationItem(R.drawable.icon_two_s, getString(R.string.article)).setInactiveIconResource(R.drawable.icon_two_n).setActiveColorResource(R.color.material_white);
        BottomNavigationItem item3 = new BottomNavigationItem(R.drawable.icon_three_s, getString(R.string.me)).setInactiveIconResource(R.drawable.icon_three_n).setActiveColorResource(R.color.material_white);
        bottomNavigation
                .addItem(item1)
                .addItem(item2)
                .addItem(item3)
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigation.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                viewPage.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }
    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragment.get(position);
        }

        @Override
        public int getCount() {
            return fragment.size();
        }
    }

    /**
     * 再按一次退出
     */
    @Override
    public void onBackPressed() {
        long time = System.currentTimeMillis();
        if (time - clickTime > 2000) {
            Toast.makeText(MainViewActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show();
            clickTime = time;
        }else {
            super.onBackPressed();
        }
    }
}
